#include "querymanager.h"

void QueryManager::SendAuthorization(QString login, QString pass)
{
    QList<Para> args = QList<Para>();
    Para loginarg = Para("login", login);
    if (pass != "")
    {
        Para passarg = Para("passHash", QString(QCryptographicHash::hash(pass.toUtf8(),QCryptographicHash::Md5).toHex()));
        args.append(passarg);
    }
    args.append(loginarg);
    ClientApplication::Instance()->Transport()->Send(XmlBuilder::MakeXml("authorization",args));
}

void QueryManager::SendRegistration(QString login, QString pass)
{
    Para loginarg = Para("login", login);
    Para passarg = Para("passHash", QString(QCryptographicHash::hash(pass.toUtf8(),QCryptographicHash::Md5).toHex()));
    QList<Para> args = QList<Para>();
    args.append(loginarg);
    args.append(passarg);
    ClientApplication::Instance()->Transport()->Send(XmlBuilder::MakeXml("register",args));
}

void QueryManager::SendProfileEditing(Session ss, QString pass, QString name, QString surname)
{
    QList<Para> args = QList<Para>();
    Para idarg = Para("identify", ss.getId());
    args.append(idarg);
    if (pass != "")
    {
        Para passarg = Para("newPassHash", QString(QCryptographicHash::hash(pass.toLatin1(), QCryptographicHash::Md5).toHex()));
        args.append(passarg);
    }
    if (name !="")
    {
        Para namearg = Para("name", name);
        args.append(namearg);
    }
    if (surname != "")
    {
        Para surnamearg = Para("surname",surname);
        args.append(surnamearg);
    }
    ClientApplication::Instance()->Transport()->Send(XmlBuilder::MakeXml("profile",args));
}

void QueryManager::SendUpdate(Session ss)
{
    Para idarg = Para("identify",ss.getId());
    QList<Para> args = QList<Para>();
    args.append(idarg);
    ClientApplication::Instance()->Transport()->Send(XmlBuilder::MakeXml("update",args));
}

void QueryManager::SendRoomsList(Session ss)
{
    Para idarg = Para("identify",ss.getId());
    QList<Para> args = QList<Para>();
    args.append(idarg);
    ClientApplication::Instance()->Transport()->Send(XmlBuilder::MakeXml("roomsList",args));
}

void QueryManager::SendJoinRoom(Session ss, QString room, QString pass)
{
    QList<Para> args = QList<Para>();
    Para idarg = Para("identify",ss.getId());
    Para roomarg = Para("room",room);
    args.append(roomarg);
    args.append(idarg);
    if (pass != "")
    {
        Para passarg = Para("passHash", QString(QCryptographicHash::hash(pass.toLatin1(), QCryptographicHash::Md5).toHex()));
        args.append(passarg);
    }
    ClientApplication::Instance()->Transport()->Send(XmlBuilder::MakeXml("joinRoom",args));
}

void QueryManager::SendGetUsers(Session ss, QString room)
{
    Para idarg = Para("identify",ss.getId());
    QList<Para> args = QList<Para>();
    Para roomarg = Para("room",room);
    args.append(roomarg);
    args.append(idarg);
    ClientApplication::Instance()->Transport()->Send(XmlBuilder::MakeXml("getUsers",args));
}

void QueryManager::SendGetMessages(Session ss, QString room)
{
        QList<Para> args = QList<Para>();
    Para idarg = Para("identify",ss.getId());
    Para roomarg = Para("room",room);
    args.append(roomarg);
    args.append(idarg);
    ClientApplication::Instance()->Transport()->Send(XmlBuilder::MakeXml("getMessages",args));
}

void QueryManager::SendMessage(Session ss, QString room, QString text)
{
        QList<Para> args = QList<Para>();
    Para idarg = Para("identify",ss.getId());
    Para roomarg = Para("room",room);
    Para textarg = Para("text",text);
    args.append(roomarg);
    args.append(idarg);
    args.append(textarg);
    ClientApplication::Instance()->Transport()->Send(XmlBuilder::MakeXml("sendMessage",args));
}

void QueryManager::SendSetRule(Session ss, QString room, QString user, QString rule)
{
    QList<Para> args = QList<Para>();
    Para idarg = Para("identify",ss.getId());
    Para roomarg = Para("room",room);
    Para userarg = Para("user",user);
    Para rulearg = Para("rule", rule);
    args.append(roomarg);
    args.append(idarg);
    args.append(userarg);
    args.append(rulearg);
    ClientApplication::Instance()->Transport()->Send(XmlBuilder::MakeXml("setRule",args));
}

void QueryManager::SendEditRoom(Session ss, QString room, QString descr, QString pass)
{
    QList<Para> args = QList<Para>();
    Para idarg = Para("identify",ss.getId());
    Para roomarg = Para("room",room);
    Para descrarg = Para("descr",descr);
    if (pass != "")
    {
        Para passarg = Para("passHash", QString(QCryptographicHash::hash(pass.toLatin1(), QCryptographicHash::Md5).toHex()));
        args.append(passarg);
    }
    args.append(roomarg);
    args.append(idarg);
    args.append(descrarg);
    ClientApplication::Instance()->Transport()->Send(XmlBuilder::MakeXml("editRoom",args));
}

void QueryManager::SendLeave(Session ss, QString room)
{
    QList<Para> args = QList<Para>();
    Para idarg = Para("identify",ss.getId());
    Para roomarg = Para("room",room);
    args.append(roomarg);
    args.append(idarg);
    ClientApplication::Instance()->Transport()->Send(XmlBuilder::MakeXml("leave",args));
}

void QueryManager::SendDisconnect(Session ss)
{
    QList<Para> args = QList<Para>();
    Para idarg = Para("identify",ss.getId());
    args.append(idarg);
    ClientApplication::Instance()->Transport()->Send(XmlBuilder::MakeXml("disconnect",args));
}
